/**
 *
 */
package admin.mode;

import java.util.ArrayList;
import java.util.HashMap;

import base.Power;

/**
 * @author qsj
 *
 */
public class MenuMode {
	public ArrayList<HashMap<String,String>> getTopMenuList(Power power) throws Exception{
		String sql = "select * from houseclub_topmenu";
		return power.getListFromSql(sql);
	}
	
	public ArrayList<HashMap<String,String>> getLeftMenuList(Power power) throws Exception{
		String sql = "select * from houseclub_leftmenu";
		return power.getListFromSql(sql);
	}
	
	public HashMap<String,String> getLeftMenuById(Power power,String lm_id) throws Exception{
		String sql = "select * from houseclub_leftmenu where lm_id="+lm_id;
		return power.getMapFromSql(sql);
	}
	
	public ArrayList<HashMap<String,String>> getTopMenuListByPerm(Power power,String roleId) throws Exception{
		String sql = "select * from houseclub_topmenu as a,houseclub_role_permission as b where b.role_id="+roleId+" and substring_index(a.tm_perm,',',1)=b.uri order by tm_sort,tm_id";
		return power.getListFromSql(sql);
	}
	
	
	public ArrayList<HashMap<String,String>> getLeftMenuByTopMenuId(Power power,String topMenuId) throws Exception{
		String sql = "select * from houseclub_leftmenu where tm_id="+topMenuId+" order by lm_sort,lm_id";
		return power.getListFromSql(sql);
	}
	
	
	public ArrayList<HashMap<String,String>> getLeftMenuByPermAndTopMenuId(Power power,String roldId,String topMenuId) throws Exception{
		String sql = "select * from houseclub_leftmenu as a,houseclub_role_permission as b where a.tm_id="+topMenuId+" and b.role_id="+roldId+" and substring_index(a.lm_perm,',',1)=b.uri order by lm_sort";
		return power.getListFromSql(sql);
	}
	
	
	public HashMap<String,String> getTopMenuById(Power power,String tm_id) throws Exception{
		String sql = "select * from houseclub_topmenu where tm_id="+tm_id;
		return power.getMapFromSql(sql);
	}
	
	public boolean passwordUpdate(Power power,AdminMode admin) throws Exception{
		String oldpassword = power.getParameter("oldpassword");
//		MD5Util cipher = new MD5Util();		
//		oldpassword = cipher.generatePassword(oldpassword);
		String password = power.getParameter("password");
//		password = cipher.generatePassword(password);
		String userid = power.getParameter("userid");
		HashMap<String,String> map = power.getMapFromSql("select * from u_user where userid="+userid);
		if(map.get("USERPASSWORD").equals(oldpassword)){
			if(power.executeUpdate("update u_user set userpassword='"+password+"' where userid="+userid)>0){
				return true;
			}
		}
		return false;
	}
	
	public boolean userinfoUpdate(Power power,AdminMode admin) throws Exception{
			String userid = power.getParameter("userid");
			String username = power.getParameter("username");
			String email = power.getParameter("email");
			String realname = power.getParameter("realname");
			String gender = power.getParameter("gender");
			String remark = power.getParameter("remark");
			String blogurl = power.getParameter("blogurl");
			String address = power.getParameter("address");
			
			String sql = "update u_user set username='"+username+"',email='"+email+"',remark='"+remark+"',blogurl='"+blogurl+"',address='"+address+"',realname='"+realname+"',sex='"+gender+"' where userid="+userid;
			if(power.executeUpdate(sql)>-1){
				return true;
			}
			return false;
	}
	
	public HashMap<String,String> getUserInfoById(Power power,AdminMode admin,String id) throws Exception{
		String sql = "select * from u_user where userid="+id;
		return power.getMapFromSql(sql);
	}
	
}
